package com.example.demo.security.service;

import com.example.demo.security.bean.SecurityResourceBean;
import com.example.demo.security.bean.SecurityRoleBean;
import com.example.demo.security.bean.SecurityRoleResourceBean;
import com.example.demo.security.repository.SecurityRoleRepository;
import com.example.demo.security.repository.SecurityRoleResourceRepository;
import com.example.demo.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class SecurityRoleService extends BaseService<SecurityRoleBean,SecurityRoleRepository> {


    @Autowired
    SecurityRoleResourceService roleResourceService;

    @Autowired
    SecurityResourceServer resourceServer;

    public void save(SecurityRoleBean bean, String[] roleResourceBeanSet) {

        Collection<Long> resCollection = Arrays.stream(roleResourceBeanSet).mapToLong(Long::valueOf).boxed().collect(Collectors.toList());

        List<SecurityResourceBean> list = resourceServer.repository.findAll(resCollection);



        if (bean.getId()!=0){


            SecurityRoleBean temp = getById((int) bean.getId());
            bean = temp;
            Set<SecurityRoleResourceBean> oldRoleResource = temp.getRoleResourceBeanSet();

            Set<SecurityRoleResourceBean> finalOldRoleResource = oldRoleResource;
            Set<SecurityRoleResourceBean> newRoleResource =  roleResourceService.createRoleResourceBean(list);

            Set<SecurityRoleResourceBean> finalNewRoleResource = newRoleResource;

//            oldRoleResource.retainAll(newRoleResource);
            // 求交集

            oldRoleResource = oldRoleResource.stream().filter(o->
                  finalNewRoleResource.stream().anyMatch(n->
                      o.getResource().getId()==n.getResource().getId()
                 )
             ).collect(Collectors.toSet());


//            newRoleResource.removeAll(oldRoleResource);
          Set<SecurityRoleResourceBean> removeSet = new HashSet<>();
          removeSet = finalOldRoleResource.stream().filter(n->!finalNewRoleResource.stream().anyMatch(o->
                          o.getResource().getId()==n.getResource().getId()
                  )
          ).collect(Collectors.toSet());

            // 求新的和旧的差集
            /**
             * 这里用removeAll移除 只会把roid置为null,并不能删除这条记录,感觉应该还是和练级存在关系
             */
//            bean.getRoleResourceBeanSet().removeAll(removeSet);
            roleResourceService.repository.deleteInBatch(removeSet);


            newRoleResource = newRoleResource.stream().filter(n->
                    !finalOldRoleResource.stream().anyMatch(o->
                            o.getResource().getId()==n.getResource().getId()
                    )
            ).collect(Collectors.toSet());


            bean.getRoleResourceBeanSet().addAll(newRoleResource);

        }else{
            //新的角色
           Set<SecurityRoleResourceBean> setroleresources = roleResourceService.createRoleResourceBean(list);
            bean.setRoleResourceBeanSet(setroleresources);
            bean.setRoleName("ROLL_"+new Date().getTime());
        }

        save(bean);
    }



}
